package com.jw.service;

import com.jw.dao.*;
import com.jw.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author jiangwang
 * @Created by 2021/9/26 17:33
 * @Description: TODO
 */
@Service
public class UserService
{
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    /**
     * 根据用户名查询用户
     *
     * @param username 用户名
     * @return 用户
     */
    public SysUser findByUsername(String username)
    {
        SysUser user = new SysUser();
        SysUserExample example = new SysUserExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<SysUser> userList = sysUserMapper.selectByExample(example);
        if (userList.isEmpty())
        {
            return null;
        }
        for (SysUser tbUser : userList)
        {
            user = tbUser;
        }
        return user;
    }


    /**
     * 查询用户的角色
     *
     * @param id 用户id
     * @return 用户的角色
     */
    public List<SysRole> findRolesById(int id)
    {
        SysUser userInfo = sysUserMapper.selectByPrimaryKey(id);
        if (userInfo == null)
        {
            throw new RuntimeException("该用户不存在");
        }
        List<SysRole> roles = new ArrayList<>();
        SysUserRoleExample userRoleExample = new SysUserRoleExample();
        userRoleExample.createCriteria().andUidEqualTo(userInfo.getUid());
        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.selectByExample(userRoleExample);
        List<Integer> rids = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sysUserRoleList))
        {
            for (SysUserRole sysUserRole : sysUserRoleList)
            {
                rids.add(sysUserRole.getRoleId());
            }
            if (!CollectionUtils.isEmpty(rids))
            {
                for (Integer rid : rids)
                {
                    SysRole sysRole = sysRoleMapper.selectByPrimaryKey(rid);
                    if (sysRole != null)
                    {
                        roles.add(sysRole);
                    }
                }
            }
        }
        return roles;
    }


    /**
     * 查询用户的权限
     *
     * @param roles 用户的角色
     * @return 用户的权限
     */
    public List<SysPermission> findPermissionByRoles(List<SysRole> roles)
    {
        List<SysPermission> permissions = new ArrayList<>();
        if (!CollectionUtils.isEmpty(roles))
        {
            Set<Integer> permissionIds = new HashSet<>();//存放菜单id
            List<SysRolePermission> sysRolePermissions;
            for (SysRole role : roles)
            {
                SysRolePermissionExample sysRolePermissionExample = new SysRolePermissionExample();
                sysRolePermissionExample.createCriteria().andRoleIdEqualTo(role.getId());
                sysRolePermissions = sysRolePermissionMapper.selectByExample(sysRolePermissionExample);
                if (!CollectionUtils.isEmpty(sysRolePermissions))
                {
                    for (SysRolePermission sysRolePermission : sysRolePermissions)
                    {
                        permissionIds.add(sysRolePermission.getPermissionId());
                    }
                }
            }
            if (!CollectionUtils.isEmpty(permissionIds))
            {
                for (Integer permissionId : permissionIds)
                {
                    SysPermission permission = sysPermissionMapper.selectByPrimaryKey(permissionId);
                    if (permission != null)
                    {
                        permissions.add(permission);
                    }
                }
            }
        }
        return permissions;
    }
}
